package com.tangyee.person;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.tangyee.person.db.PersonInfo;
import com.tangyee.person.view.CircleImageView;
import com.tangyee.R;
import com.tangyee.person.db.PersonInfo;
import com.tangyee.person.view.CircleImageView;
import com.tangyee.person_risk_assessment.RiskAssessmentResultActivity;

import org.litepal.LitePal;
import org.litepal.tablemanager.Connector;

import java.io.ByteArrayOutputStream;
import java.util.List;

public class PersonFragment extends Fragment implements View.OnClickListener{
    private TextView username;
    CircleImageView headImage;
    PersonInfo personInfo;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_person, container, false);
        view.findViewById(R.id.person_picture).setOnClickListener(this);
        view.findViewById(R.id.set).setOnClickListener(this);
        view.findViewById(R.id.linearlayout1).setOnClickListener(this);
        view.findViewById(R.id.linearlayout2).setOnClickListener(this);
        view.findViewById(R.id.linearlayout3).setOnClickListener(this);
        view.findViewById(R.id.linearlayout4).setOnClickListener(this);
        view.findViewById(R.id.linearlayout5).setOnClickListener(this);
        view.findViewById(R.id.linearlayout6).setOnClickListener(this);
        view.findViewById(R.id.linearlayout7).setOnClickListener(this);
        view.findViewById(R.id.linearlayout8).setOnClickListener(this);
        view.findViewById(R.id.linearlayout9).setOnClickListener(this);
        view.findViewById(R.id.linearlayout10).setOnClickListener(this);
        view.findViewById(R.id.linearlayout11).setOnClickListener(this);
        view.findViewById(R.id.linearlayout13).setOnClickListener(this);
        view.findViewById(R.id.linearlayout14).setOnClickListener(this);
        view.findViewById(R.id.linearlayout15).setOnClickListener(this);
        view.findViewById(R.id.linearlayout16).setOnClickListener(this);
        view.findViewById(R.id.linearlayout17).setOnClickListener(this);
        view.findViewById(R.id.linearlayout18).setOnClickListener(this);
        view.findViewById(R.id.linearlayout19).setOnClickListener(this);
        view.findViewById(R.id.linearlayout20).setOnClickListener(this);
        view.findViewById(R.id.linearlayout21).setOnClickListener(this);
        username=(TextView)view.findViewById(R.id._person_name);
        headImage=view.findViewById(R.id.person_picture);
        LitePal.getDatabase();//创建数据库
        personInfo=new PersonInfo();//数据表
        Bitmap bitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.testhead);
        //把图片转换字节流
        byte[] images=img(bitmap);
        personInfo.setName("糖宝_12328");
        personInfo.setHeadshot(images);
        personInfo.save();
        headImage.setImageBitmap(bitmap);
        username.setText(personInfo.getName());
        return view;
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.person_picture:
                //头像
                Intent intent2 = new Intent(getActivity(),HeadPicture.class);
                startActivity(intent2);
                break;
            case R.id.set:
                //设置
                Intent intent1 = new Intent(getActivity(),setActivity.class);
                startActivity(intent1);
                break;
            case R.id.linearlayout1:
                //基本信息
                Intent intent = new Intent(getActivity(),BasicInfomationActivity.class);
                startActivity(intent);
                break;
            case R.id.linearlayout2:
                Toast.makeText(PersonFragment.this.getContext(), "你点击了关联医生", Toast.LENGTH_SHORT).show();
                break;
            case R.id.linearlayout3:
                Toast.makeText(PersonFragment.this.getContext(), "你点击了血糖管理", Toast.LENGTH_SHORT).show();
                break;
            case R.id.linearlayout4:
                Toast.makeText(PersonFragment.this.getContext(), "你点击了血脂管理", Toast.LENGTH_SHORT).show();
                break;
            case R.id.linearlayout5:
                Toast.makeText(PersonFragment.this.getContext(), "你点击了运动管理", Toast.LENGTH_SHORT).show();
                break;
            case R.id.linearlayout6:
                Toast.makeText(PersonFragment.this.getContext(), "你点击了体重管理", Toast.LENGTH_SHORT).show();
                break;
            case R.id.linearlayout7:
                Toast.makeText(PersonFragment.this.getContext(), "你点击了血压管理", Toast.LENGTH_SHORT).show();
                break;
            case R.id.linearlayout8:
                Toast.makeText(PersonFragment.this.getContext(), "你点击了用药管理", Toast.LENGTH_SHORT).show();
                break;
            case R.id.linearlayout9:
                Toast.makeText(PersonFragment.this.getContext(), "你点击了情绪管理", Toast.LENGTH_SHORT).show();
                break;
            case R.id.linearlayout10:
                Toast.makeText(PersonFragment.this.getContext(), "你点击了随访管理", Toast.LENGTH_SHORT).show();
                break;
            case R.id.linearlayout11:
                Toast.makeText(PersonFragment.this.getContext(), "你点击了并发症管理", Toast.LENGTH_SHORT).show();
                break;
            case R.id.linearlayout13:
                Intent intent13 = new Intent(PersonFragment.this.getContext(), RiskAssessmentResultActivity.class);
                startActivity(intent13);
                break;
            case R.id.linearlayout14:
                Toast.makeText(PersonFragment.this.getContext(), "你点击了肥胖风险", Toast.LENGTH_SHORT).show();
                break;
            case R.id.linearlayout15:
                Toast.makeText(PersonFragment.this.getContext(), "你点击了心血管疾病", Toast.LENGTH_SHORT).show();
                break;
            case R.id.linearlayout16:
                Toast.makeText(PersonFragment.this.getContext(), "你点击了热卡套餐", Toast.LENGTH_SHORT).show();
                break;
            case R.id.linearlayout17:
                Toast.makeText(PersonFragment.this.getContext(), "你点击了套餐记录", Toast.LENGTH_SHORT).show();
                break;
            case R.id.linearlayout18:
                Toast.makeText(PersonFragment.this.getContext(), "你点击了用户菜谱", Toast.LENGTH_SHORT).show();
                break;
            case R.id.linearlayout19:
                Toast.makeText(PersonFragment.this.getContext(), "你点击了菜品收藏", Toast.LENGTH_SHORT).show();
                break;
            case R.id.linearlayout20:
                Toast.makeText(PersonFragment.this.getContext(), "你点击了饮品收藏", Toast.LENGTH_SHORT).show();
                break;
            case R.id.linearlayout21:
                Toast.makeText(PersonFragment.this.getContext(), "你点击了水果收藏", Toast.LENGTH_SHORT).show();
                break;
        }
    }
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if ((isVisibleToUser && isResumed())) {
            onResume();
        } else if (!isVisibleToUser) {
            onPause();
        }
    }
    @Override
    public void onResume() {
        super.onResume();
        if (getUserVisibleHint()) {
            CircleImageView headimg=getView().findViewById(R.id.person_picture);
            TextView name=getView().findViewById(R.id._person_name);
            personInfo=LitePal.findFirst(PersonInfo.class);
            if(personInfo.getHeadshot()!=null){
                Bitmap b=Bytes2Bitmap(personInfo.getHeadshot());
                headimg.setImageBitmap(b);
            }
            name.setText(personInfo.getName());
        }
    }
    @Override
    public void onPause() {
        super.onPause();
    }
    //图片转二进制
    private byte[] img(Bitmap bitmap){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }
    //二进制转图片
    public Bitmap Bytes2Bitmap(byte[] b) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        } else {
            return null;
        }
    }
}
